from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class Notification(models.Model):
    """通知消息"""
    NOTIFICATION_TYPE_CHOICES = [
        ('system', '系统通知'),
        ('order', '订单通知'),
        ('promotion', '促销通知'),
        ('inventory', '库存通知'),
        ('comment', '评论通知'),
        ('security', '安全通知'),
    ]
    
    PRIORITY_CHOICES = [
        ('low', '低'),
        ('medium', '中'),
        ('high', '高'),
        ('urgent', '紧急'),
    ]

    notification_type = models.CharField(max_length=20, choices=NOTIFICATION_TYPE_CHOICES, verbose_name='通知类型')
    title = models.CharField(max_length=200, verbose_name='通知标题')
    content = models.TextField(verbose_name='通知内容')
    priority = models.CharField(max_length=10, choices=PRIORITY_CHOICES, default='medium', verbose_name='优先级')
    
    # 目标用户
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications', null=True, blank=True)
    is_global = models.BooleanField(default=False, verbose_name='是否全局通知')
    
    # 状态
    is_read = models.BooleanField(default=False, verbose_name='是否已读')
    read_at = models.DateTimeField(null=True, blank=True, verbose_name='阅读时间')
    
    # 有效期
    valid_from = models.DateTimeField(verbose_name='有效期开始')
    valid_to = models.DateTimeField(verbose_name='有效期结束')
    
    # 附加数据
    extra_data = models.JSONField(default=dict, blank=True, verbose_name='附加数据')
    action_url = models.URLField(blank=True, verbose_name='操作链接')
    
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'notifications'
        verbose_name = '通知'
        verbose_name_plural = '通知'
        ordering = ['-created_at']

    def __str__(self):
        return self.title


class Announcement(models.Model):
    """公告"""
    ANNOUNCEMENT_TYPE_CHOICES = [
        ('system', '系统公告'),
        ('maintenance', '维护公告'),
        ('promotion', '促销活动'),
        ('update', '更新公告'),
        ('policy', '政策变更'),
    ]
    
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('published', '已发布'),
        ('archived', '已归档'),
    ]

    title = models.CharField(max_length=200, verbose_name='公告标题')
    content = models.TextField(verbose_name='公告内容')
    announcement_type = models.CharField(max_length=20, choices=ANNOUNCEMENT_TYPE_CHOICES, verbose_name='公告类型')
    
    # 状态
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='状态')
    
    # 优先级
    priority = models.IntegerField(default=0, verbose_name='优先级')
    is_urgent = models.BooleanField(default=False, verbose_name='是否紧急')
    
    # 有效期
    publish_at = models.DateTimeField(verbose_name='发布时间')
    expire_at = models.DateTimeField(null=True, blank=True, verbose_name='过期时间')
    
    # 版本控制
    version = models.CharField(max_length=20, default='1.0', verbose_name='版本号')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='revisions')
    
    # 统计
    view_count = models.IntegerField(default=0, verbose_name='浏览次数')
    
    # 富文本内容
    rich_content = models.JSONField(default=dict, blank=True, verbose_name='富文本内容')
    
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='created_announcements')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'announcements'
        verbose_name = '公告'
        verbose_name_plural = '公告'
        ordering = ['-priority', '-created_at']

    def __str__(self):
        return self.title


class HelpArticle(models.Model):
    """帮助中心文章"""
    CATEGORY_CHOICES = [
        ('account', '账户问题'),
        ('order', '订单问题'),
        ('payment', '支付问题'),
        ('shipping', '配送问题'),
        ('return', '退换货'),
        ('technical', '技术问题'),
        ('other', '其他问题'),
    ]

    title = models.CharField(max_length=200, verbose_name='文章标题')
    content = models.TextField(verbose_name='文章内容')
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, verbose_name='分类')
    
    # 排序
    sort_order = models.IntegerField(default=0, verbose_name='排序')
    
    # 状态
    is_published = models.BooleanField(default=True, verbose_name='是否发布')
    
    # 统计
    view_count = models.IntegerField(default=0, verbose_name='浏览次数')
    helpful_count = models.IntegerField(default=0, verbose_name='有帮助数')
    not_helpful_count = models.IntegerField(default=0, verbose_name='无帮助数')
    
    # 相关文章
    related_articles = models.ManyToManyField('self', blank=True, symmetrical=False, verbose_name='相关文章')
    
    # 关键词
    keywords = models.CharField(max_length=500, blank=True, verbose_name='关键词')
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'help_articles'
        verbose_name = '帮助文章'
        verbose_name_plural = '帮助文章'
        ordering = ['sort_order', '-created_at']

    def __str__(self):
        return self.title


class UserAgreement(models.Model):
    """用户协议"""
    AGREEMENT_TYPE_CHOICES = [
        ('terms', '服务条款'),
        ('privacy', '隐私政策'),
        ('refund', '退款政策'),
        ('shipping', '配送政策'),
    ]
    
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('active', '生效中'),
        ('deprecated', '已废弃'),
    ]

    title = models.CharField(max_length=200, verbose_name='协议标题')
    content = models.TextField(verbose_name='协议内容')
    agreement_type = models.CharField(max_length=20, choices=AGREEMENT_TYPE_CHOICES, verbose_name='协议类型')
    
    version = models.CharField(max_length=20, verbose_name='版本号')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='状态')
    
    # 生效时间
    effective_date = models.DateField(verbose_name='生效日期')
    
    # 富文本内容
    rich_content = models.JSONField(default=dict, blank=True, verbose_name='富文本内容')
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_agreements'
        verbose_name = '用户协议'
        verbose_name_plural = '用户协议'
        ordering = ['-effective_date']

    def __str__(self):
        return f"{self.title} - v{self.version}"


class PushNotification(models.Model):
    """推送通知配置"""
    PLATFORM_CHOICES = [
        ('web', '网页'),
        ('ios', 'iOS'),
        ('android', 'Android'),
        ('all', '全部平台'),
    ]

    title = models.CharField(max_length=100, verbose_name='推送标题')
    content = models.TextField(verbose_name='推送内容')
    platform = models.CharField(max_length=20, choices=PLATFORM_CHOICES, default='all', verbose_name='推送平台')
    
    # 目标用户
    target_users = models.JSONField(default=list, blank=True, verbose_name='目标用户ID列表')
    is_broadcast = models.BooleanField(default=False, verbose_name='是否广播')
    
    # 定时推送
    scheduled_at = models.DateTimeField(null=True, blank=True, verbose_name='定时推送时间')
    
    # 状态
    is_sent = models.BooleanField(default=False, verbose_name='是否已发送')
    sent_at = models.DateTimeField(null=True, blank=True, verbose_name='发送时间')
    
    # 统计
    success_count = models.IntegerField(default=0, verbose_name='成功数')
    failure_count = models.IntegerField(default=0, verbose_name='失败数')
    
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='push_notifications')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'push_notifications'
        verbose_name = '推送通知'
        verbose_name_plural = '推送通知'
        ordering = ['-created_at']

    def __str__(self):
        return self.title
